﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace FFmpegVideoClip.Lib.UtilityControlsTools
{
    [ValueConversion(typeof(IEnumerable<Fragment>), typeof(string))]
    internal class ClipTotalTimeConvert : IMultiValueConverter
    {
        /// <summary>
        /// 因为直接绑定【ObservableCollection<Fragment> Fragments】 内部变化不获得实时通知，因此多绑定一个Fragments.Count用以获得通知但是转换中用不到它
        /// </summary>
        /// <param name="values"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if(values== null || values.Length < 1)
                return Tools.TimeShortZero;
            if (values[0] is IEnumerable<Fragment> Fragments)
                return new TimeSpan(Fragments.Sum(p => p.FragmentDuration.Ticks)).ToString(Tools.TimeSpan_hhmmssfff);
            return Tools.TimeShortZero;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
